﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Consejo.Monitoring.ApplicationHooks
{
    public class Values : IApplicationHook
    {
        public object GetManagementValue(string dataSourceType, string dataSourceName,string dataSourceValue, out Object objectType)
        {
            string query = string.Empty;
            if (dataSourceType == "svc")
            {
                query = string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", dataSourceName);
                ManagementObjectSearcher moquery = new ManagementObjectSearcher(query);
                ManagementObjectCollection queryCollection = moquery.Get();
                //return queryCollection;
                {

                    foreach (ManagementObject service in queryCollection)
                    {
                        //
                        //string serviceId = Convert.ToString(service["DisplayName"]);
                        bool serviceResult = Convert.ToBoolean(service["Started"]);
                        string servicePath = Convert.ToString(service["PathName"]);
                        UInt32 ProcessId = Convert.ToUInt32(service["ProcessId"]);
                        string ServiceType = Convert.ToString(service["ServiceType"]);
                        string StartMode = Convert.ToString(service["StartMode"]);
                        string State = Convert.ToString(service["State"]);
                        string Status = Convert.ToString(service["Status"]);
                        string SystemName = Convert.ToString(service["SystemName"]);
                        objectType = Convert.ToBoolean(service["Started"]);
                        //object p = SystemName;
                    }
                    

                }
            }
            if (dataSourceType == "Resource")
            {
                
                switch (dataSourceName)
                {
                    case "FreeDiskSpace":
                        double _freeSpace = 0.00;
                        String FreeDiskSpaceQuery = string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID =" + "'" + dataSourceValue + "'" + "");
                        ManagementObjectSearcher moquery = new ManagementObjectSearcher(FreeDiskSpaceQuery);
                        //ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE DeviceID =" + "'" + _DeviceID + "'" + "");
                        ManagementObjectCollection queryCollection = moquery.Get();
                        foreach (ManagementObject disk in queryCollection)
                        {
                            try
                            {
                                string device = Convert.ToString(disk["DeviceID"]);
                                double _freeSpace1 = Convert.ToDouble(disk["FreeSpace"]) / 1024 / 1024 / 1024;
                                string _freeSpace2 = _freeSpace1.ToString("N3");
                                _freeSpace = Convert.ToDouble(_freeSpace2);
                                objectType = _freeSpace;
                            }
                            catch
                            {
                                _freeSpace = 0.00;
                            }
                        }
                        break;

                    case "CPU":
                        double cpuLoad = 0.00;
                        string CPUquery = string.Format("SELECT * FROM Win32_Processor");
                        ManagementObjectSearcher CPUmoquery = new ManagementObjectSearcher(CPUquery);
                        ManagementObjectCollection CPUqueryCollection = CPUmoquery.Get();
                        foreach (ManagementObject cpu in CPUqueryCollection)
                        {
                            try
                            {
                                cpuLoad = Convert.ToDouble(cpu["LoadPercentage"]);
                                objectType = cpuLoad;

                            }
                            catch
                            {
                                cpuLoad = 0.00;
                                objectType = cpuLoad;
                            }

                        }
                        break;
                    case "ResponseTime":
                        UInt32 PingVal = 0;
                        bool IsReachable = false;
                        string Target = dataSourceValue;
                        SelectQuery query3 = new SelectQuery("Win32_PingStatus", string.Format("Address='{0}'", Target));
                        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query3);
                        try
                        {
                            foreach (ManagementObject result in searcher.Get())
                            {
                                PingVal = Convert.ToUInt32(result["ResponseTime"]);
                                IsReachable = (result["StatusCode"] != null && (0 == (UInt32)result["StatusCode"]));
                                //string[] response =new string {IsReachable , PingVal};
                                objectType = PingVal;
                               

                            }
                        }
                        catch
                        {
                            PingVal = 0;
                        }
                        break;

                    case "Memory":
                        UInt64 MemoryVal = UInt64.MinValue;
                        UInt64 FreeMemoryVal = UInt64.MinValue;
                        string Memoryquery = string.Format("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");
                        ManagementObjectSearcher Memorymoquery = new ManagementObjectSearcher(Memoryquery);
                        ManagementObjectCollection MemoryqueryCollection = Memorymoquery.Get();
                        {
                            try
                            {
                                foreach (ManagementObject result in queryCollection)

                                    MemoryVal = Convert.ToUInt64(result["TotalPhysicalMemory"]) / 1024 / 1024;
                                objectType = MemoryVal;


                            }
                            catch
                            {
                                MemoryVal = 0;
                                objectType = MemoryVal;
                            }
                        }
                        break;

                        case "FreeMemory":
                            string FreeMemoryquery = string.Format("SELECT FreePhysicalMemory FROM Win32_OperatingSystem");
                            ManagementObjectSearcher FreeMemorymoquery = new ManagementObjectSearcher(query);
                            ManagementObjectCollection FreeMemoryqueryCollection = FreeMemorymoquery.Get();
                            {
                                try
                                {
                                    foreach (ManagementObject result in FreeMemoryqueryCollection)


                                        FreeMemoryVal = Convert.ToUInt64(result["FreePhysicalMemory"]) / 1024;
                                    return FreeMemoryVal;
                                }
                                catch
                                {

                                    FreeMemoryVal = 0;
                                }
                            }
                            break;

                        }
                }

            }
        
        }

        }
    
